package com.example.myanimator.utils

import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

/**
 * @author tgw
 * @date 2021/7/26
 * @describe
 */
object TimeFormatUtils {

    val YY_MM_DD_HH_MM_SS = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH)
    val HH_MM = SimpleDateFormat("HH:mm", Locale.ENGLISH)
    val YY_MM_DD = SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH)
    val HH_MM_SS = SimpleDateFormat("HH:mm:ss", Locale.ENGLISH)


    /*
     * 将时间戳转换为时间
     */
    fun stampToDate(time: Long, format: SimpleDateFormat): String? {
        val res: String
        val date = Date(time)
        res = format.format(date)
        return res
    }

    /*
     * 将时间转换为时间戳
     */
    @Throws(ParseException::class)
    fun dateToStamp(time: String?, format: SimpleDateFormat): String? {
        val date = format.parse(time)
        val ts = date.time
        return ts.toString()
    }


    /**将秒换为时分秒00:00:00*/
    fun secToTime(time: Int): String {
        var timeStr: String = ""
        var hour = 0
        var minute = 0
        var second = 0
        if (time <= 0) {
            return "00:00:00"
        } else {
            minute = time / 60
            if (minute < 60) {
                second = time % 60
                timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second)
            } else {
                hour = minute / 60
                if (hour > 99) {
                    return "99:59:59"
                }
                minute %= 60
                second = time - hour * 3600 - minute * 60
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second)
            }
        }
        return timeStr
    }

    /**文案补0*/
    private fun unitFormat(i: Int): String {
        var retStr: String? = null
        retStr = if (i >= 0 && i < 10) "0" + Integer.toString(i) else "" + i
        return retStr
    }
}